#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: category_sharing
    Description:
        Author: lhw
        Date: 2020/6/5
-----------------------------------
"""

from rest_framework.views import APIView
from EducationContent import models
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from django.db.models import Min, Max
from Common.logger import app_log
from Common.response import resp


# class CategoryCopy(APIView):
#
#     # authentication_classes = [JwtAuthorizationAuthentication, ]
#     def post(self, request, *args, **kwargs):
#         try:
#             new_hospital_id = request.data.get("new_hospital_id", "")
#             old_hospital_id = request.data.get("old_hospital_id", "")
#             category_id_list = request.data.get("category_id_list", "")
#             copy_from_id = request.data.get("copy_from_cid", "")
#             copy_to_parent_id = request.data.get("copy_to_parent_id", "")
#             # category part copy
#             all_data = models.EducationCategory.objects.filter(cid__in=category_id_list,
#                                                                hospital_id=old_hospital_id).all()
#             for single_data in all_data:
#                 data = single_data.category_to_json()
#                 data['hospital_id'] = new_hospital_id
#                 models.EducationCategory.objects.create(**data)
#             current_max_order = models.EducationCategory.objects.filter(
#                 hospital_id=new_hospital_id,
#                 parent_id=copy_to_parent_id
#             ).all().aggregate(Max('order_id'))['order_id__max']
#             if current_max_order is None:
#                 current_max_order = 0
#             order_id = current_max_order + 1
#             models.EducationCategory.objects.filter(cid=copy_from_id, hospital_id=new_hospital_id).update(
#                 parent_id=copy_to_parent_id, order_id=order_id)
#             # content part copy
#             content_data = models.EducationFeeds.objects.filter(category_id__in=category_id_list,
#                                                                 hospital_id=old_hospital_id).all()
#             for single in content_data:
#                 single_data = single.educationFeeds_details_to_json()
#                 single_data.pop('id')
#                 single_data['hospital_id'] = new_hospital_id
#                 models.EducationFeeds.objects.create(**single_data)
#             return Response(
#                 {
#                     'error_code': 0,
#                     'message': "Success"
#                 }
#             )
#         except Exception as e:
#             app_log.exception(e)
#             return resp(-1, e.args)

class CategoryCopy(APIView):
    authentication_classes = [JwtAuthorizationAuthentication, ]

    def post(self, request, *args, **kwargs):
        try:
            hospital_id = request.data.get("hospital_id", "")
            content_list = request.data.get("content_list", "")
            category_list = request.data.get("category_list", "")
            new_hospital_category_id = request.data.get("new_hospital_category_id", "")
            # 原有的这家医院的所有分类和文章删除
            # models.EducationCategory.objects.filter(hospital_id=hospital_id).delete()
            models.EducationFeeds.objects.filter(hospital_id=hospital_id, is_delete=False).update(is_delete=True)
            # 创建这家医院的所有分类
            for single_category_data in category_list:
                id = single_category_data.pop("id")
                single_category_data.pop("title")
                category_data = models.EducationCategory.objects.filter(id=id).values("title", "title_SP").first()
                single_category_data["title"] = category_data["title"]
                single_category_data["title_SP"] = category_data["title_SP"]
                models.EducationCategory.objects.create(**single_category_data)
            # 删除这家医院的所有分类
            for single_id in new_hospital_category_id:
                models.EducationCategory.objects.filter(id=single_id).delete()
            # 创建这家医院的所有文章
            for single_content_data in content_list:
                single_content = models.EducationFeeds.objects.filter(id=single_content_data["id"]).first()
                data = single_content.educationFeeds_details_to_json()
                data.pop('id')
                data['hospital_id'] = hospital_id
                data['category_id'] = single_content_data["category_id"]
                data['content_order'] = single_content_data["content_order"]
                data['source'] = data["source"]['id']
                data["is_delete"] = False
                models.EducationFeeds.objects.create(**data)
            # 删除垃圾数据
            for it in content_list:
                record = models.EducationFeeds.objects.filter(id=it['id']).first()
                if record:
                    if record.hospital_id == hospital_id and record.is_delete:
                        record.delete()
            return Response({
                'error_code': 0,
                'message': "Success"
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)
